<template>
  <section class="todoapp">
    <TodoHeader :arr="list" @add="addfn"></TodoHeader>
    <TodoMain :arr="showarr" @del="delFn"></TodoMain>
    <TodoFooter :arr="showarr" @sai="change" @getclear="clearfn"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from "./components/todoheard";
import TodoMain from "./components/todobody";
import TodoFooter from "./components/todofloot";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      shai: "all",
      list: JSON.parse(localStorage.getItem("zylist")) || [],
      // list: [
      //   { id: 100, name: "吃饭", isDone: true },
      //   { id: 201, name: "睡觉", isDone: false },
      //   { id: 103, name: "打豆豆", isDone: true },
      // ],
    };
  },
  methods: {
    addfn(todoname) {
      this.list.push({
        // id:
        //   this.list.legth === 0 ? 100 : this.list[this.list.length - 1].id + 1,
        id: this.list.length + 1,
        name: todoname,
        isDone: false,
      });
    },
    delFn(index) {
      console.log(index);
      this.list.splice(index, 1);
    },
    change(str) {
      this.shai = str;
    },
    clearfn() {
      this.list = this.list.filter((item) => item.isDone === false);
    },
  },
  computed: {
    showarr() {
      if (this.shai === "ok") {
        return this.list.filter((item) => item.isDone === true);
      } else if (this.shai === "no") {
        return this.list.filter((item) => item.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  watch: {
    list: {
      handler(newVal) {
        localStorage.setItem("zylist", JSON.stringify(newVal));
      },
      deep: true,
    },
  },
};
</script>